package com.map
{
	import com.Dispatcher;
	import com.ModuleCommand;
	import com.util.Point3D;
	import com.util.iso.IsoUtil;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class MapDatas
	{
		public static const common_path:String = "E:/AS3Learning/MyGame/elements/xian_yuan_cun/";
		public var mapImgArr:Array;
		public static const ROW_NUM:int = 10;
		public static const COL_NUM:int = 15;
		public static const SLICE_SIZE:int = 300;
		public static const GRID_HEIGHT:int = 50;
		public static const ISO_GRID_SIZE:Number = 200 * Math.sqrt(5) >> 2;
		public var mapGrid:MapGrid;
		
		public var viewHeight:int;
		public var viewWidth:int;
		private var _mainSprite:Sprite;
		
		public function MapDatas()
		{
			mapImgArr = new Array(ROW_NUM * COL_NUM);
			mapGrid = new MapGrid;
			trace("run init mapdatas");
		}
		
		public function set mainSprite(value:Sprite):void {
			_mainSprite = value;
			resizeView();
			mainSprite.stage.addEventListener(Event.RESIZE, resizeView);	
		}
		
		public function get mainSprite():Sprite {
			return _mainSprite;
		}
		
		private function resizeView(e:Event = null):void {
			viewHeight = _mainSprite.stage.stageHeight;
			viewWidth = _mainSprite.stage.stageWidth;
			Dispatcher.dispatch(ModuleCommand.STAGE_RESIZE);
		}
		
		private static var _instance:MapDatas;
		public static function getInstance():MapDatas {
			return _instance ||= new MapDatas;
		}
		
		public function getOffset(p:Point3D):Point {
			var flatP:Point = IsoUtil.isoToFlat(p);
			flatP.x -= mapGrid.oriX;
			flatP.y -= mapGrid.oriY;
//			return new Point(flatP.x - mapGrid.oriX, flatP.y - mapGrid.oriY);
			var offsetX:Number = Math.max(flatP.x - viewWidth / 2, 0);
			var offsetY:Number = Math.max(flatP.y - viewHeight / 2, 0);
			offsetX = Math.min(offsetX, map_width - viewWidth);
			offsetY = Math.min(offsetY, map_height - viewHeight);
			
			return new Point(offsetX, offsetY);
		}
		
		public function getViewIndexRec(p:Point3D):Rectangle {
			var offsetP:Point = getOffset(p);
			var col:int = offsetP.x / SLICE_SIZE;
			var row:int = offsetP.y / SLICE_SIZE;
			var endCol:int = (offsetP.x + viewWidth) / SLICE_SIZE;
			var endRow:int = (offsetP.y + viewHeight) / SLICE_SIZE;
			var cCol:int = Math.max(0, col - 1);	//以下为rec的各种多一圈的修正值
			var cRow:int = Math.max(0, row - 1);
			var cEndCol:int = Math.min(map_width / SLICE_SIZE, endCol + 1);
			var cEndRow:int = Math.min(map_height / SLICE_SIZE, endRow + 1);
			return new Rectangle(cCol, cRow, cEndCol - cCol, cEndRow - cRow);	
			//多一圈，切片显示有误差，窗口未缩放情况下，误差源于速度，速度越大，误差越大
		}
		
		public var map_width:int = 4492;
		
		public var map_height:int = 2885;
		
		/*public function isInXBorder(p:Point):Boolean {	//纵向边界
			var offsetP:Point = getOffset(p);
			return (offsetP.x == 0) || (offsetP.x >= map_width - viewWidth);
		}
		
		public function isInYBorder(p:Point):Boolean {	//横向边界
			var offsetP:Point = getOffset(p);
			return (offsetP.y == 0) || (offsetP.y >= map_height - viewHeight);
		}*/
		
		public function isPointInMap(p:Point3D):Boolean {	//将超出地图边界的点放回到地图中(容差：25)
			var mapP:Point = IsoUtil.isoToFlat(p);
			mapP.x = mapP.x - mapGrid.oriX;
			mapP.y = mapP.y - mapGrid.oriY;
			return mapP.x > 25 && mapP.x < map_width - 25 && mapP.y > 25 && mapP.y < map_height - 25; 
		}
		
		public function isIsoValidPoint(p:Point3D):Boolean {	//未优化不可走点的理想状态
			return p.z > (Math.sqrt(5) >> 2) * map_width &&
				p.x > (Math.sqrt(5) >> 2) * map_width - p.z &&
				p.z < ((Math.sqrt(5) >> 2) * map_width + (Math.sqrt(5) * map_height)) &&
				p.x < ((Math.sqrt(5) >> 2) * map_width + (Math.sqrt(5) * map_height)) - p.z;
		}
		
		public function getGridVOByXz(xIndex:int, zIndex:int):GridVO {
			return mapGrid.isoTiles[zIndex][xIndex] as GridVO;
		}
	}
}